

var isBalanced = function (root) {
    return height(root) === -1 ? false : true
};

function height(root) {
    if (root === null) {
        return 0
    }
    let leftHeight = height(root.left)
    if (leftHeight === -1) {
        return -1
    }
    let rightHeight = height(root.right)
    if (rightHeight === -1) {
        return -1
    }
    if (Math.abs(leftHeight - rightHeight) > 1) {
        return -1
    } else {
        return Math.max(leftHeight, rightHeight) + 1
    }
}